from direct.gui.DirectGui import DirectLabel
from QScrolledFrame import QScrolledFrame
from Cube.gui.DirectGUIPropertiesAccess import WidthGet as FrameWidthGet
from pandac.PandaModules import TextNode


DefaltParameter_Label = dict(text_scale = 0.07,
                             text = "",
                             )

class QScrolledTextLabelFrame(QScrolledFrame):
    def __init__(self,
                 parent = None,
                 param_Label = {},
                 param_ScrolledFrame = {}):
        paramIn_Label = DefaltParameter_Label.copy()
        paramIn_Label.update(param_Label)
        textLabel = DirectLabel(**paramIn_Label)
        QScrolledFrame.__init__(self,
                                parent = parent,
                                packObject = textLabel,
                                param_ScrolledFrame = param_ScrolledFrame
                                )

        # get text object(OnscreenText) of textLabel
        txtObject = textLabel.component( textLabel.components()[0] )
        # attach method of textNode to textObject(OnscreenText in DirectLabel)
        txtObject.getTextHeight = txtObject.textNode.getHeight
        # store member
        self.textObject = txtObject
        self.textLabel = textLabel

    def ResizedCallBackHadle(self):
        txtObject = self.textObject
        # align text
        txtObject['align'] = TextNode.ALeft
        # word warp
        textWScale = txtObject['scale'][0]
        outerframeWidth = FrameWidthGet(self._QScrolledFrame)
        scrollBarWidth = self._QScrolledFrame["scrollBarWidth"]
        txtObject['wordwrap'] = int((outerframeWidth-scrollBarWidth)/textWScale)-1
        # reset canvas size
        self.ResetCanvasSize()

        # test
        #txtObject["frame"] = (1,1,1,1)

    def ResetCanvasSize(self):
        textHScale = self.textObject['scale'][1]
        textHeight = textHScale * self.textObject.getTextHeight()
        # pull text a little down
        size = (0,0,                             # setup width?
                0-textHeight-(textHScale*1.25),  # bottom
                0+(textHScale*1.25)              # top
                )
        self._QScrolledFrame['canvasSize'] = size
        self.textLabel["frameSize"] = size
        self._QScrolledFrame.verticalScroll["value"] = 1  # scrolling bar

    def appendText(self,text):
        """\
ScrolledTextFrame.appendText(text)
    Append text after current text on frame.

    Parameters:
        text       ( type = string )"""
        self.textObject.appendText("%s\n"%(text)) # workaround
        self.ResetCanvasSize()

    def setText(self, text):
        """\
ScrolledTextFrame.setText(text)
    Clear and set new text on frame.

    Parameters:
        text       ( type = string )"""
        self.textObject.setText("%s\n"%(text)) # workaround
        self.ResetCanvasSize()

    def clearText(self):
        """\
ScrolledTextFrame.clearText()
    Clear text."""
        self.textObject.clearText()
        self.ResetCanvasSize()

